//
//  MainViewController.swift
//  SwiftWeibo
//
//  Created by Lucky on 16/4/23.
//  Copyright © 2016年 Lucky. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        setupComposeBtn()
    }
    
    // MARK: - 中间按钮点击
    func click() {
        
        let composeVC = ComposeViewController()
        let nav = UINavigationController(rootViewController: composeVC)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    private func setupComposeBtn() {
        tabBar.addSubview(composeBtn)
        
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: width, height: 49)
        composeBtn.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    private func addChildViewControllers() {
        //获取Bundle中json文件中的数组数据
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        if let jsonPath = path {
            let jsonData = NSData(contentsOfFile: jsonPath)
            
            do { //可能发生异常的代码放这里
                
                //try：发生异常会跳到catch中继续执行
                //try!：发生异常直接崩溃，不用do－catch
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                
                //在swift中如果便利一个数组必须明确数组元素类型
                for dict in dictArr as! [[String: String]]{
                    addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                }
                
//                print(dictArr)
                
            } catch {
                print(error)
                
                [addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")]
                [addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")]
                [addChildViewController("NullViewController", title: "", imageName: "")]
                [addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")]
                [addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")]
            }
            
        }
    }

    private func addChildViewController(childControllerName: String, title:String, imageName:String) {
        
        let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        let controller:AnyClass? = NSClassFromString(nameSpace + "." + childControllerName)
        
        let childControllerClass = controller as! UIViewController.Type
        
        let childController = childControllerClass.init()
        
//        print(childController)
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        childController.title = title
        
        let nav = UINavigationController()
        nav.addChildViewController(childController)
        addChildViewController(nav)
    }
    
    // MARK: - 懒加载
    private lazy var composeBtn : UIButton = {
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: #selector(MainViewController.click), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
}
